<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px"
             :rules="rules"
    >
      <el-form-item label="物料号" prop="materialNo">
        <el-input
          v-model="queryParams.materialNo"
          placeholder="请输入物料号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="part_data" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="物料号" align="center" width="150" prop="materialNo" :show-overflow-tooltip="true"/>
      <el-table-column label="名称" align="left" prop="name" width="120" :show-overflow-tooltip="true"/>
      <el-table-column label="规格型号" align="left" prop="specification" width="160" :show-overflow-tooltip="true"/>
      <el-table-column label="品牌" align="left" prop="brand" width="120" :show-overflow-tooltip="true"/>
      <el-table-column label="当前库存" align="center" prop="stock" width="100" :show-overflow-tooltip="true"/>
      <el-table-column label="类型" align="center" prop="materialType" width="55" :show-overflow-tooltip="true"/>
      <el-table-column label="状态1" align="center" prop="status1" width="80" :show-overflow-tooltip="true"/>
      <el-table-column label="状态2" align="center" prop="status2" width="80" :show-overflow-tooltip="true"/>
      <el-table-column label="单价" align="center" prop="unitPrice" width="100" :show-overflow-tooltip="true"/>
      <el-table-column label="SAP数量" align="center" prop="sapStock" width="100" :show-overflow-tooltip="true"/>
      <el-table-column label="安全库存" align="center" prop="safetyStock" width="100" :show-overflow-tooltip="true"/>
      <el-table-column label="最大库存" align="center" prop="maximalStock" width="100" :show-overflow-tooltip="true"/>
      <el-table-column label="产线" align="left" prop="line" width="100" :show-overflow-tooltip="true"/>
      <el-table-column label="工位" align="left" prop="station" width="100" :show-overflow-tooltip="true"/>
      <el-table-column label="架位" align="left" prop="location" width="120" :show-overflow-tooltip="true"/>
      <!--      <el-table-column label="图片地址" align="center" prop="imgUrl"/>-->
      <!--      <el-table-column label="图纸地址" align="center" prop="drawUrl"/>-->
      <el-table-column label="备注" align="center" prop="remark" width="80" :show-overflow-tooltip="true"/>
    </el-table>
    <div style="margin-top: 30px;margin-left: 20px">
      <el-form ref="form" :model="form" label-width="100px" :rules="f_rules">
        <el-form-item label="物料号" prop="materialNo">
          <el-input v-model="form.materialNo" placeholder="请输入物料号" :style="'width:200px'" :disabled="true"/>
        </el-form-item>
        <el-form-item label="退库自产线" prop="line">
          <el-select
            v-model="form.line"
            placeholder="产线"
            clearable
            style="width: 240px"
            @change="getStationData(form.line)"
          >
            <el-option
              v-for="item in proline_data"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="退库自工位" prop="station">
          <el-select
            v-model="form.station"
            placeholder="工位"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="item in station_data"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="退库数量" prop="outNum">
          <el-input v-model.number="form.outNum" placeholder="请输入退库数量" :style="'width:200px'"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" :style="'margin-left:50px'">
        <el-button type="primary" @click="submitForm" v-hasPermi="['wms:warehouse:returnn:add']">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { getOnePart, getPartByMaterialNo } from '@/api/wms/warehouse'
import { addReturn } from '@/api/wms/w_return'
import { getProductLine, getStation } from '@/api/wms/equipment'

export default {
  name: 'Warehouse',
  data() {
    return {
      part_data: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        materialNo: null
      },
      form: {},
      // 表单校验
      rules: {
        materialNo: [
          { required: true, message: '物料号不能为空', trigger: 'blur' },
          { min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: 'blur' }
        ]
      },
      f_rules: {
        materialNo: [
          { required: true, message: '物料号不能为空', trigger: 'blur' },
          { min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: 'blur' }
        ],
        line: [
          { required: true, message: '产线不能为空', trigger: 'blur' },
          { min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: 'blur' }
        ],
        station: [
          { required: true, message: '工位不能为空', trigger: 'blur' },
          { min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: 'blur' }
        ],
        outNum: [
          { required: true, message: '退库数量不能为空', trigger: 'blur' },
          {
            type: 'number', message: '数量必须为数字值,最小值为1', min: 1
          }
        ]
      },
      proline_data: [],
      station_data: []
    }
  },
  created() {
    if (this.$route.query.id !== '' && this.$route.query.id != null) {
      this.getPart(this.$route.query.id)
    } else {
      this.loading = false
    }
    this.getProductLine()
  },
  mounted() {

  },
  methods: {
    getPart(id) {
      getOnePart(id).then(response => {
        this.queryParams.materialNo = response.data.materialNo
        this.form.materialNo = response.data.materialNo
        this.part_data = [response.data]
        this.loading = false
      })
    },
    // 表单重置
    reset() {
      this.form = {
        materialNo: null,
        line: null,
        station: null,
        outNum: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      if (this.queryParams.materialNo !== '' && this.queryParams.materialNo != null) {
        this.getPartByMaterialNo(this.queryParams.materialNo)
      } else {
        this.part_data = []
      }
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
      this.reset()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    getPartByMaterialNo(materialNo) {
      this.loading = true
      getPartByMaterialNo(materialNo).then(response => {
        this.queryParams.materialNo = response.data.materialNo
        this.part_data = [response.data]
        this.form.materialNo = response.data.materialNo
        this.loading = false
      }).catch(err => {
        this.loading = false
        this.resetForm('queryForm')
        this.part_data = []
        this.queryParams.materialNo = null
        this.form.materialNo = null
      })
    },
    getProductLine() {
      getProductLine().then(response => {
        this.proline_data = response.rows
      })
    },
    getStationData(line) {
      getStation(line).then(
        response => {
          this.station_data = response.rows
        }
      )
    },
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          addReturn(this.form).then(response => {
            this.$modal.msgSuccess('退库成功')
            this.getPartByMaterialNo(this.queryParams.materialNo)
          }).catch(err => err)
        }
      })
    },
    cancel() {
      this.resetQuery()
    }
  }
}
</script>
